package com.feilo.action;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.feilo.biz.ProductBiz;
import com.feilo.biz.impl.ProductBizImpl;
import com.feilo.entity.Product;
import com.feilo.util.ProReader;
import com.opensymphony.xwork2.ActionContext;
import com.zoo.pojo.Page;
@Controller
@Scope("prototype")
public class ProductAction {
	@Resource
	ProductBiz ps =new ProductBizImpl();
	private List<Product> products;
	private Product product;
	//闁跨喐鏋婚幏鐑姐�夐柨鐔告灮閹烽攱浼呴柨鐔告灮閹风兘鏁撻弬銈嗗闁跨喐鏋婚幏宄板絿闁跨喐鏋婚幏绌歛geInfo
	  private Page pageInfo; 
	 private int pageNo = 1; 
	  private String url ;
	int starts,ends,record,page;
	private String skipUrl;
	private String text;
	private String msg;
	String addName=(String) ActionContext.getContext().getSession().get("user");
	String ip=ServletActionContext.getRequest().getRemoteAddr();
	  private File uploadImage;//闁跨喓鐓喊澶嬪闁跨喕绶濇潏鐐闁跨喐鏋婚幏鐑芥晸娓氥儳銆嬮幏锟�
	  private String uploadImageContentType;//闁跨喓鐓喊澶嬪闁跨喍鑼庣涵閿嬪闁跨喐鏋婚幏鐑芥晸閺傘倖瀚归柨鐔告灮閹凤拷
	  private String uploadImageFileName;//闁跨喓鐓喊澶嬪闁跨喍鑼庣涵閿嬪闁跨喐鏋婚幏鐑芥晸閺傘倖瀚归柨鐕傛嫹
	/**
	 * 闁跨喐鏋婚幏鐤祮闁跨喐鏋婚幏鐑芥晸閺傘倖瀚归崫渚�鏁撻崣顐ｅ敾閹凤拷
	 */
	  public String productList(){
			List<Product> list=ps.getProducts();
			int pageSize = Integer.parseInt(ProReader.getInstance().getProperty("pageSize"));
			int showCount = Integer.parseInt(ProReader.getInstance().getProperty("showCount"));
	        pageInfo = new Page(pageNo, pageSize, list.size(),showCount);   
	        starts=pageInfo.getRecordStart()-1;
	        ends=pageInfo.getRecordEnd();
	        record=list.size();
	        page=record % pageSize == 0 ? record / pageSize : record / pageSize + 1;
	        if(list.isEmpty()){
	        	starts=0;
	        	ends=0;
	        }
	        products = list.subList(starts,ends);    
	        url="product_productList.do";   
			return "productList";
		}
		/**
		 * �����Ʒ�б�
		 */
		public String search(){
			List<Product> list = ps.getProductsBySerch(product.getP_name());
			int pageSize = Integer.parseInt(ProReader.getInstance().getProperty("pageSize"));
			int showCount = Integer.parseInt(ProReader.getInstance().getProperty("showCount"));
	        pageInfo = new Page(pageNo, pageSize, list.size(),showCount);  
	        starts=pageInfo.getRecordStart()-1;
	        ends=pageInfo.getRecordEnd();
	        if(list.isEmpty()){
	        	starts=0;
	        	ends=0;
	        }
	        record=list.size();
	        page=record % pageSize == 0 ? record / pageSize : record / pageSize + 1;
	        products = list.subList(starts,ends);   
	        url="product_search.do";   
			return "productList";
		}
	/**
	 *闁跨喐鏋婚幏宄版惂闁跨喐鏋婚幏鐑芥晸閺傘倖瀚�
	 * @return
	 */
	public String add(){
		String realpath = ServletActionContext.getServletContext().getRealPath("/uploadImages");
		File file = new File(realpath);
		if(!file.exists()) file.mkdirs();
		try {
			String ext = uploadImageFileName.substring(uploadImageFileName.lastIndexOf(".")+1,uploadImageFileName.length());
			String filename = UUID.randomUUID().toString()+ "."+ext;//闁跨喐鏋婚幏鐑芥晸閺傘倖瀚归柨鐔惰寧绾攱瀚归柨鐔告灮閹风兘鏁撻敓锟�
			FileUtils.copyFile(uploadImage, new File(file, filename));
			product.setP_image(filename);
			product.setP_addtime(new Date());
			ps.save(product);
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "message";
}

	/**
	 * 闁跨喐鏋婚幏鐤祮闁跨喐鏋婚幏鐑芥晸閺傘倖瀚归崫渚�鏁撻弬銈嗗闁跨喐鏋婚幏鐑姐�夐柨鐔告灮閹凤拷
	 * @return
	 */
	public String addUI(){
		return "productAdd";
	}
	/**
	 * 闁跨喐鏋婚幏宄版惂闁跨喐鏋婚幏鐑芥晸閿燂拷
	 * @return
	 */
	public String productView(){
		this.product=ps.getProduct(product.getId());
		return "productView";
	}
	/**
	 * 闁跨喐鏋婚幏鐤祮闁跨喐鏋婚幏鐑芥晸閺傘倖瀚归崫渚�鏁撳ú浣界帆
	 * @return
	 */
	public String editUI(){
		this.product=ps.getProduct(product.getId());
		return "edit";
	}
	/**
	 *闁跨喐鏋婚幏宄版惂闁跨喐纾ユ潏锟�
	 * @return
	 */
	public String edit(){
		String realpath = ServletActionContext.getServletContext().getRealPath("/uploadImages");
		File file = new File(realpath);
		if(!file.exists()) file.mkdirs();
		try {
			String ext = uploadImageFileName.substring(uploadImageFileName.lastIndexOf(".")+1,uploadImageFileName.length());
			String filename = UUID.randomUUID().toString()+ "."+ext;//闁跨喐鏋婚幏鐑芥晸閺傘倖瀚归柨鐔惰寧绾攱瀚归柨鐔告灮閹风兘鏁撻敓锟�
			FileUtils.copyFile(uploadImage, new File(file, filename));
			product.setP_image(filename);
			product.setP_addtime(new Date());
			ps.update(product);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return "message";
	}

	/*getter闁跨喐鏋婚幏绌漞tter闁跨喐鏋婚幏鐑芥晸閺傘倖瀚�*/
	public List<Product> getProducts() {
		return products;
	}
	public void setProducts(List<Product> products) {
		this.products = products;
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}

	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	public String getSkipUrl() {
		return skipUrl;
	}
	public void setSkipUrl(String skipUrl) {
		this.skipUrl = skipUrl;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public File getUploadImage() {
		return uploadImage;
	}
	public void setUploadImage(File uploadImage) {
		this.uploadImage = uploadImage;
	}
	public String getUploadImageContentType() {
		return uploadImageContentType;
	}
	public void setUploadImageContentType(String uploadImageContentType) {
		this.uploadImageContentType = uploadImageContentType;
	}
	public String getUploadImageFileName() {
		return uploadImageFileName;
	}
	public void setUploadImageFileName(String uploadImageFileName) {
		this.uploadImageFileName = uploadImageFileName;
	}	
	
}
